
package com.b1.taskmanager.start.gui;

import com.b1.taskmanager.api.ContactList;
import java.awt.AWTError;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class ShowContact extends JFrame
{
    private JButton button;
    private JPanel panel;
    private JTable table;
    private DefaultTableModel modelTable;
    private JScrollPane scroll;
    private JLabel label = new JLabel();
    private ContactList cl = new ContactList();
    
    public TableModel getTableModel()
    {
        return modelTable;
    }
    public void setTableModel(TableModel tm)
    {
        table.setModel(tm);
    }
    
    public ShowContact() 
    {
        super("Contact List");
        this.setSize(602, 200);
        try 
        {
            java.awt.Toolkit jToolkit = java.awt.Toolkit.getDefaultToolkit();
            Dimension screenSize = jToolkit.getScreenSize();
            this.setLocation((int) (screenSize.getWidth()/4), (int) (screenSize.getHeight()/4));
        } 
        catch (AWTError ex) 
        {
        }
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        panel = new JPanel();
        panel.setLayout(null);
        
        String columnNames[] = {"id","First Name","Last Name","Phone"};
        String dataValues[][] =  cl.listToString();
        modelTable = new DefaultTableModel(dataValues,columnNames);
        
        table = new JTable(modelTable);
        ListSelectionModel selModel = table.getSelectionModel();
        selModel.addListSelectionListener(new ShowContact.tableSelectionEvent());
        scroll = new JScrollPane(table);
        scroll.setSize(600,100);
        scroll.setLocation(0,0);
        table.setPreferredScrollableViewportSize(new Dimension(600, 100));
        panel.add(scroll);
        
        button = new JButton("Add Contact");
        button.setSize(120, 40);
        button.setLocation(120, 120);
        button.addActionListener(new ShowContact.ButtonEventListenerAddContact());
        panel.add(button);
        
        button = new JButton("Delete Contact");
        button.setSize(120, 40);
        button.setLocation(250, 120);
        button.addActionListener(new ShowContact.ButtonEventListenerDelContact());
        panel.add(button);
        
        button = new JButton("Change Contact");
        button.setSize(125, 40);
        button.setLocation(380, 120);
        button.addActionListener(new ShowContact.ButtonEventListenerChangeContact());
        panel.add(button);
        
        this.setContentPane(panel);
    }
    
    class ButtonEventListenerAddContact implements ActionListener 
    {

        public void actionPerformed(ActionEvent e) 
        {
            //JOptionPane.showMessageDialog(null,"Button ADD was pressedn", "Output",JOptionPane.PLAIN_MESSAGE);
            AddWindow app = new AddWindow(1);
            app.setVisible(true);
            setVisible(false);
        }
    }
    class ButtonEventListenerDelContact implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
            if(label.getText().equals("")) JOptionPane.showMessageDialog(null,"don't select ", "Output",JOptionPane.PLAIN_MESSAGE);
            else 
            {
                //JOptionPane.showMessageDialog(null, "Button DEL " + label.getText(), "Output", JOptionPane.PLAIN_MESSAGE);
                if(cl.delContact(Integer.parseInt(label.getText())))
                {
                    int sel = table.getSelectedRow(); 
                    modelTable.removeRow(sel);
                    table.setModel(modelTable);
                }
                else JOptionPane.showMessageDialog(null, "Mistake DEL " + label.getText(), "Output", JOptionPane.PLAIN_MESSAGE);
            }
        }
        
    }
    class ButtonEventListenerChangeContact implements ActionListener 
    {

        public void actionPerformed(ActionEvent e) 
        {
            if(label.getText().equals("")) JOptionPane.showMessageDialog(null,"don't select ", "Output",JOptionPane.PLAIN_MESSAGE);
            else 
            {
                //JOptionPane.showMessageDialog(null,"Button Change "+label.getText(), "Output",JOptionPane.PLAIN_MESSAGE);
                ChangeWindow chw= new ChangeWindow(1,Integer.parseInt(label.getText()));
                chw.setVisible(true);
                setVisible(false);
            }
        }
    }
    
    class tableSelectionEvent implements ListSelectionListener
    {
            public void valueChanged(ListSelectionEvent e) 
            {
                String result = "";
                int[] selectedRows = table.getSelectedRows();
                for(int i = 0; i < selectedRows.length; i++) 
                {
                    result += table.getModel().getValueAt(selectedRows[i], 0);
                }
                label.setText(result);
                label.setVisible(false);
                panel.add(label);
            }
    }
    
}
